package com.tobeing.calendar.lib;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by ToBeing on 2017/10/13/013.
 */

public class ToBeingCalendar extends LinearLayout {
    private ImageView mIVPrev, mIVNext;
    private TextView mTVData;
    private GridView mGVCalendar;
    private CalendarDayClickListener mCalendarDayClickListener;
    private Date mSelectDate = new Date();

    private Calendar mCalendar = Calendar.getInstance();

    public ToBeingCalendar(Context context) {
        this(context, null);
    }

    public ToBeingCalendar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ToBeingCalendar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.to_being_calendar, this);
        initViews();
        initEvents();
        notifyDataChange();
    }

    private void initViews(){
        mIVPrev = (ImageView) findViewById(R.id.iv_prev);
        mTVData= (TextView) findViewById(R.id.tv_data);
        mIVNext = (ImageView) findViewById(R.id.iv_next);
        mGVCalendar= (GridView) findViewById(R.id.gv_calendar);
    }

    private void initEvents(){
        mIVPrev.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mCalendar.add(Calendar.MONTH, -1);
                notifyDataChange();
            }
        });

        mIVNext.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mCalendar.add(Calendar.MONTH, 1);
                notifyDataChange();
            }
        });

        mGVCalendar.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToBeingDate date = (ToBeingDate) parent.getItemAtPosition(position);
                setSelectDate(date.getDate());
                if(mCalendarDayClickListener != null){
                    mCalendarDayClickListener.onClick(date.getDate());
                }
            }
        });
    }

    private void notifyDataChange(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
        mTVData.setText(sdf.format(mCalendar.getTime()));

        List<ToBeingDate> listDate = new ArrayList<>();
        Calendar calendar = (Calendar) mCalendar.clone();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int prevDays = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        calendar.add(Calendar.DAY_OF_MONTH, -prevDays);
        int maxDateCount = 6 * 7;
        while (listDate.size() < maxDateCount){
            ToBeingDate date = new ToBeingDate();
            date.setDate(calendar.getTime());
            if(mSelectDate.getYear() == date.getDate().getYear() && mSelectDate.getMonth() == date.getDate().getMonth() &&
                    mSelectDate.getDate() == date.getDate().getDate()){
                date.setSelected(true);
            }else {
                date.setSelected(false);
            }
            listDate.add(date);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        mGVCalendar.setAdapter(new CalendarAdapter(getContext(), mCalendar, listDate));
    }

    public void setSelectDate(Date date){
        mSelectDate = date;
        mCalendar.setTime(date);
        notifyDataChange();
    }

    public void setCalendarDayClickListener(CalendarDayClickListener listener){
        mCalendarDayClickListener = listener;
    }
}
